package com.wcb.test.controller;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wcb.test.bean.PageBean;
import com.wcb.test.bean.Product;
import com.wcb.test.bean.SaleRecord;
import com.wcb.test.bean.User;
import com.wcb.test.service.QueryDataService;

/**
 * @desc
 * @date 2014-05-01
 * @author knight Wang
 * 
 */

@Controller
@RequestMapping("/show")
public class ShowDataController {
	
	//private Log log = LogFactory.getLog(UploadController.class);
	@Autowired
	QueryDataService queryDataService;
	
	
	/**
	 * 查询用户列表
	 * 
	 * @param excelfile
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/userlist")
	public String userlist(HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		
		int pageNo = 0; 
		int pageSize = 10;
		try{
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}catch (Exception e) {
			pageNo = 0;
		}
		try{
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}catch (Exception e) {
			pageSize = 10;
		}
		
		try {
			 PageBean<User> usersPage = queryDataService.getUserPage(pageNo, pageSize);
			 map.put("pageModel", usersPage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "userlist";
	}
	
	
	@RequestMapping(value = "/productlist")
	public String productlist(HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		int pageNo = 0; 
		int pageSize = 10;
		try{
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}catch (Exception e) {
			pageNo = 0;
		}
		
		try{
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}catch (Exception e) {
			pageSize = 10;
		}
		try {
			PageBean<Product> productPage = queryDataService.getProductPage(pageNo, pageSize);
			map.put("pageModel", productPage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "productlist";
	}
	
	@RequestMapping(value = "/viewSaleRecordlist")
	public String saleRecordlist(HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		int pid = 0;
		int pageNo = 0; 
		int pageSize = 10;
		
		try{
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}catch (Exception e) {
			pageNo = 0;
		}
		
		try{
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}catch (Exception e) {
			pageSize = 10;
		}
		
		try{
			pid = Integer.parseInt(request.getParameter("pid"));
		}catch (Exception e) {
			pid = 0;
		}
		 
		try {
			PageBean<SaleRecord> productPage = queryDataService.getSaleRecordPage(pageNo, pageSize,0,pid);
			map.put("pageModel", productPage);
			map.put("product", queryDataService.getProductById(pid));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "saleRecordlist";
	}
	
	
	@RequestMapping(value = "/showindex", method=RequestMethod.GET)
	public String showindex(HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		return "main";
	}
	
	
	@RequestMapping(value = "/showChart")
	@SuppressWarnings("unchecked")
	public String showChart(HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		
		String address = request.getParameter("address");
		if(address == null || "".equals(address)) address = "湖南省";
		
		
		Date startDate = queryDataService.getMaxMothStartDate();
		Date preDate = new Date(startDate.getTime()-1000l);
		
		List<Map<String, Object>> topSales = queryDataService.getMaxMonthTopProfitProductList(address);
		List<Map<String, Object>> preTopSales = queryDataService.getPreMonthTopProfitProductList(address);
		
		StringBuilder dataBudiler = new StringBuilder("[");
		for (Iterator iterator = topSales.iterator(); iterator.hasNext();) {
			Map<String, Object> data = (Map<String, Object>) iterator.next();
			dataBudiler.append("[");
			dataBudiler.append("'");
			dataBudiler.append(data.get("pname")==null?"":data.get("pname").toString());
			dataBudiler.append("',");
			dataBudiler.append(data.get("profit")==null?"":data.get("profit").toString());
			dataBudiler.append("]");
			dataBudiler.append(",");
		}
		if(dataBudiler.indexOf(",")>-1){
			dataBudiler.deleteCharAt(dataBudiler.lastIndexOf(","));
		}
		dataBudiler.append("]");
		
		
		StringBuilder dataBudiler1 = new StringBuilder("[");
		for (Iterator iterator = preTopSales.iterator(); iterator.hasNext();) {
			Map<String, Object> data = (Map<String, Object>) iterator.next();
			dataBudiler1.append("[");
			dataBudiler1.append("'");
			dataBudiler1.append(data.get("pname")==null?"":data.get("pname").toString());
			dataBudiler1.append("',");
			dataBudiler1.append(data.get("profit")==null?"":data.get("profit").toString());
			dataBudiler1.append("]");
			dataBudiler1.append(",");
		}
		if(dataBudiler1.indexOf(",")>-1){
			dataBudiler1.deleteCharAt(dataBudiler1.lastIndexOf(","));
		}
		dataBudiler1.append("]");
		
		
		map.put("recentMonth", startDate); 
		map.put("recentData", dataBudiler.toString()); 
		map.put("preMonth", preDate); 
		map.put("preDate", dataBudiler1.toString()); 
		
		List<Map<String, Object>> provsList = queryDataService.getAddressList();
		//System.out.println("provsList length =="+provsList.size() +" 1 = "+provsList.get(0));
		map.put("selectaddress", address);
		map.put("addresslist", provsList);
		return "showChart";
	}
	
	
	 
}
